import oscP5.*;
import netP5.*;
import tuioZones.*;

GamingArea area;
int verschiebenUm = 5;

void setup(){
  size(1024,768);
  // create a draggable, throwable, and scalable zone for a photo
  area=new GamingArea(this);
  
  noFill();
  smooth();
}

void draw(){
  background(0);
  
  GamingZone gz = area.getGamingZone();
  /*
  stroke(gz.getR(), gz.getG(), gz.getB());
  strokeWeight(10);
  strokeJoin(ROUND);
  noFill();
  rect(gz.getX(), gz.getY(), gz.getWidth(), gz.getHeight());
  */
  printDropAreas(); 
  
  for(int i=0; i < area.getTouchZones().size(); i++) {
    TouchZone tz = area.getTouchZones().get(i);
    noStroke();
    noFill();
    
               
      if (tz.getPuzzle()==null) {
          translate(tz.getX(), tz.getY());
          rotate(radians(tz.getAngle()));
          PImage img = tz.getCard().getBgImage();
          image(img, 0, 0);
          strokeWeight(2);
          stroke(0);
          rect(0,0,(int)tz.getWidth(),(int)tz.getHeight());
          rotate(radians(-tz.getAngle()));
          translate(-tz.getX(), -tz.getY());

          if(tz.getRotationPoint() != null) {
            strokeWeight(10);
            stroke(123,0,0);
            ellipse(tz.getRotationPoint().getX(),tz.getRotationPoint().getY(),20,20);
          }
        } else {                  
          tz.setWidth(GamingArea.CARD_WIDTH*2);                  
          translate(tz.getX(), tz.getY());
          rotate(radians(tz.getAngle()));                                    
          PImage img1 = tz.getPuzzle().getCA().getBgImage();                  
          PImage img2 = tz.getPuzzle().getCB().getBgImage();
          image(img1, 0, 0);
          image(img2, GamingArea.CARD_WIDTH,0);
          strokeWeight(2);
          stroke(0);
          rect(0,0,(int)tz.getWidth(),(int)tz.getHeight());
          rotate(radians(-tz.getAngle()));
          translate(-tz.getX(), -tz.getY());
          
          if(tz.getRotationPoint() != null) {
              strokeWeight(10);
              stroke(123,0,0);
              ellipse(tz.getRotationPoint().getX(),tz.getRotationPoint().getY(),20,20);
          }

    }
  }
    /* PUZZLES in drop Areas */
    for(int i=0; i < area.getDropAreas().length; i++) {
            DropArea d = area.getDropAreas()[i];
            /*stroke(d.getR(),d.getG(),d.getB());
            strokeWeight(10);
            strokeJoin(ROUND);
            noFill();
            rect(d.getX(),d.getY(), d.getWidth(), d.getHeight());*/
            //display all finished puzzles
            ArrayList<Puzzle> puzzles = d.getFinishedPuzzles();

            if (puzzles.size()!=0) {
                int counter = -1;                                

                for (int z = 0; z < puzzles.size(); z++) {
                  Puzzle p = puzzles.get(z);
                    counter ++;
                    PImage image = p.getCA().getBgImage();
                    PImage image2 = p.getCB().getBgImage();
                    int strokeWeight = 2;
                    translate(d.getTranslationPoint().getX(), d.getTranslationPoint().getY());
                    rotate(radians(d.getAngle()));
                    image(image, (counter/3) * strokeWeight +((counter)/3)*d.getPuzzleWidth(), (counter%3)*d.getPuzzleHeight(), d.getPuzzleWidth()/2, d.getPuzzleHeight() - 2*strokeWeight);
                    image(image2, (counter/3) * strokeWeight +((counter)/3)*d.getPuzzleWidth() + d.getPuzzleWidth()/2, (counter%3)*d.getPuzzleHeight(), d.getPuzzleWidth()/2, d.getPuzzleHeight() - 2*strokeWeight);

                    rotate(-radians(d.getAngle()));
                    translate(-d.getTranslationPoint().getX(), -d.getTranslationPoint().getY());
                    
                    // -1, because we start with 0
                    if (counter == d.getMaxPuzzles()-1) {

                        if (d.getR2()!=0)
                            d.setR2(d.getR2()-3);
                        if (d.getG2()!=0)
                            d.setG2(d.getG2()-3);
                        if (d.getB2()!=0)
                            d.setB2(d.getB2()-3);

                        fill(d.getR2(),d.getG2(),d.getB2());
                        rect(d.getX(), d.getY(), d.getWidth(), d.getHeight());
                        noFill();
                        
                        if ((d.getR2()<=0)&&(d.getG2()<=0)&&(d.getB2()<=0)) {
                            d.resetInsideRGB();
                            d.removeAllPuzzles();
                        }                                                
                    }
                }
                             
            }
          }
  
  for(int i=0; i < area.getTouchPoints().size(); i++) {
    TouchPoint tp = area.getTouchPoints().get(i);
    noStroke();
    fill(100,100,100);
    try {
    ellipse(tp.getX(), tp.getY(), 20,20);
    }catch(Exception e) {
      
    }
  }

}

void printDropAreas() {
  for(int i=0; i < area.getDropAreas().length; i++) {
    DropArea d = area.getDropAreas()[i];
    stroke(d.getR(),d.getG(),d.getB());
    strokeWeight(10);
    strokeJoin(ROUND);
    noFill();
    rect(d.getX(),d.getY(), d.getWidth(), d.getHeight());
  }
}





